package com.kingtsoft.study.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.model.StudyHomeworkDetail;
import com.kingtsoft.study.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kingtsoft.study.model.StudyHomework;
import com.kingtsoft.study.persist.StudyHomeworkMapper;
import com.kingtsoft.study.service.StudyHomeworkService;

/**
 * Title:
 * Description:
 * Copyright: Copyright (c) 2017-03-30 下午3:01
 * Company: wondersgroup.com
 *
 * @author jerry.pan(潘健雷)
 * @version 1.0
 */
@Service
public class StudyHomeworkServiceImpl implements StudyHomeworkService {

    @Autowired
    StudyHomeworkMapper homeworkMapper;

    /**
     * 获取作业
     *
     * @param id 作业主键
     * @return 作业
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public StudyHomework load(String id){
        return homeworkMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页获取老师布置的作业的列表
     *
     * @return pageInfo
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public PageInfo<StudyHomework> listPage(PageInfo page, StudyHomework studyHomework) {
        //分页查询,紧跟着的第一个方法会被分页
        PageHelper.startPage(page);
        List<StudyHomework> list = homeworkMapper.listAll(studyHomework);
        //用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 获取老师布置的作业的列表
     *
     * @return list
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public List<StudyHomework> listAll(StudyHomework studyHomework) {
        return homeworkMapper.listAll(studyHomework);
    }


    /**
     *
     * @Title: addHomework
     * @Description: 老师发布作业，保存homework
     * @author: shenlx
     * @param studyHomework
     * @return: void
     */
	@Override
	public void addHomework(StudyHomework studyHomework) {
		homeworkMapper.insert(studyHomework);
	}


    /**
     * 保存上传文件
     *
     * @author jerry.pan
     * @date 2017-04-01 15:38:22
     */
    public StudyHomework saveUpload(StudyHomework studyHomework){
        //保存到作业表
        homeworkMapper.updateByPrimaryKeySelective(studyHomework);
        //返回
        return studyHomework;
    }

    /**
     * 删除上传文件
     *
     * @param
     * @author jerry.pan
     * @date 2017-04-01 15:38:22
     */
    public Map deleteFile(String id) {
        Map resultMap = Maps.newHashMap();
        StudyHomework homework = this.load(id);
        if (homework == null) {
            resultMap.put("describable", DescribableEnum.FILE_NOT_EXIST);
            return resultMap;
        }
        //1.删除硬盘中的文件
        String relativePath = homework.getPath();
        try {
            String fullPath = FileUtil.getPath(relativePath);
            FileUtil.deleteFile(fullPath);
        } catch (IOException e) {
            resultMap.put("describable", DescribableEnum.FILE_DEL_ERROR);
            return resultMap;
        }
        //2.删除表中的数据
        homework.setFilename(null);
        homework.setFileSize(null);
        homework.setSuffix(null);
        homework.setPath(null);
        homeworkMapper.updateByPrimaryKey(homework);
        resultMap.put("describable", DescribableEnum.SUCCESS);
        return resultMap;
    }

}
